import React from "react";
import ReactDOM from "react-dom/client";

import "./index.css";
import App from "./App";

const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(<App />);

console.log("hello");

class DataSource {
  constructor() {
    // 订阅者
    this.listener = [];
  }
  // 订报纸
  addChangeListener(callback) {
    this.listener.push(callback);
  }
  // 发布消息
  publish(data) {
    for (let i = 0; i < this.listener.length; i++) {
      // 对应每一个订报纸的人
      const listener = this.listener[i];
      listener(data);
    }
  }
  // 取消订阅
  removeChangeListener(callback) {
    let index = this.listener.indexOf(callback);
    console.log(index);
    this.listener.splice(index, 1);
  }
}
const data_source = new DataSource();
function fn(data) {
  console.log("真开心", data);
}
data_source.addChangeListener(fn);

data_source.addChangeListener((data) => {
  console.log("不开心", data);
});
data_source.removeChangeListener(fn);
data_source.publish("hello");
data_source.publish("hi");
